#include "login.h"
#include "ticketing.h"
#include "myfont.h"
#include "admin.h"

//获取日期
void get_date_str(char *buffer, size_t size) {
    // 获取当前系统时间
    time_t t = time(NULL);
    struct tm *tm_info = localtime(&t);
    snprintf(buffer, size, "%d.%d.%d",
             tm_info->tm_year + 1900,
             tm_info->tm_mon + 1,
             tm_info->tm_mday);
}

//检查是否是重复用户
bool check_user(const char *check_username)//true为重复，false为不重复
{
    FILE *fp_check = fopen("appuser/user.txt", "r");
    if(fp_check == NULL) {
        return false;  // 文件不存在，无重复用户
    }
    
    char line[100];
    bool found = false;
    
    while(fgets(line, sizeof(line), fp_check) != NULL) 
    {
        // 跳过空行
        if(strlen(line) <= 1) continue;
        
        char username[20];
        char password[20];
        
        // 安全解析行内容
        if(sscanf(line, "user:%19s %19s", username, password) == 2) {
            if(strcmp(username, check_username) == 0) {
                found = true;
                break;
            }
        }
    }
    
    fclose(fp_check);
    return found;
}

//检查用户名与密码是否匹配
bool check_user_password(const char *username, const char *password)//true为匹配，false为不匹配
{
    FILE *fp_check = fopen("appuser/user.txt", "r");
    if(fp_check == NULL) 
    {
        return false;  // 文件不存在，无用户信息
    }

    char line[100];
    bool found = false;

    while(fgets(line, sizeof(line), fp_check) != NULL)
    {
        // 跳过空行
        if(strlen(line) <= 1) continue;

        char check_username[20];
        char check_password[20];

        // 安全解析行内容
        if(sscanf(line, "user:%19s %19s", check_username, check_password) == 2)
        {
            if(strcmp(username, check_username) == 0 && strcmp(password, check_password) == 0)
            {
                found = true;
                break;
            }
        }
    }

    fclose(fp_check);
    return found;
}

//注册事件
void go_logon_cb(lv_event_t * e)
{
    char *username = lv_textarea_get_text(logon_username_input);//用户输入的用户名
    char *password = lv_textarea_get_text(logon_password_input);//用户输入的密码
    char *password2 = lv_textarea_get_text(logon_password_input2);//用户输入的密码2

    if(strcmp(username, "") == 0 || strcmp(password, "") == 0 || strcmp(password2, "") == 0)//判断用户名和密码是否为空
    {
        logon_error_popup = lv_msgbox_create(login,"出错了","用户名或密码为空!",NULL,true);//错误提示弹窗
        lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0); 
        lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
        lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        return;
    }
    else if(strcmp(password, password2) != 0)//判断两次输入的密码是否一致
    {
        logon_error_popup = lv_msgbox_create(login,"出错了","两次密码不一致!",NULL,true);//错误提示弹窗
        lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0);
        lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
        lv_textarea_add_text(logon_password_input, "");//清空密码输入框
        lv_textarea_add_text(logon_password_input2, "");//清空密码输入框2
        lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        return;
    }
    else
    {
        DIR *dir = opendir("appuser");//打开appuser文件夹
        if(dir == NULL)//判断文件夹是否存在
        {
            
            mkdir("appuser", 0777);//创建appuser文件夹
            FILE *fp = fopen("appuser/user.txt", "w");//创建user.txt文件
            fprintf(fp, "user:%s %s", username, password);//写入用户名和密码
            lv_obj_clean(login);
            login_init();
            logon_error_popup = lv_msgbox_create(login,"注册成功","恭喜您，成为新用户!",NULL,true);//成功提示弹窗
            lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0);
            lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
            lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
            fclose(fp);//关闭文件
        }
        else
        {
            FILE *fp = fopen("appuser/user.txt", "a");//打开user.txt文件
            if(fp == NULL)//判断文件是否存在
            {
                
                mkdir("appuser", 0777);//创建appuser文件夹
                fp = fopen("appuser/user.txt", "w");//创建user.txt文件
                fprintf(fp, "user:%s %s", username, password);//写入用户名和密码
                lv_obj_clean(login);
                login_init();
                logon_error_popup = lv_msgbox_create(login,"注册成功","恭喜您，成为新用户!",NULL,true);//成功提示弹窗
                lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0);
                lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
                lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
                fclose(fp);//关闭文件
            }
            else
            {
                if(check_user(username) == true)//判断用户名是否重复
                {
                    logon_error_popup = lv_msgbox_create(login,"出错了","该用户名已被使用",NULL,true);//错误提示弹窗
                    lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0);
                    lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
                    fclose(fp);//关闭文件
                    lv_textarea_add_text(logon_password_input, "");//清空密码输入框
                    lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
                    return;
                }
                else
                {
                    fprintf(fp, "\nuser:%s %s", username, password);//写入用户名和密码
                    lv_obj_clean(login);
                    login_init();
                    logon_error_popup = lv_msgbox_create(login,"注册成功","恭喜您，成为新用户!",NULL,true);//成功提示弹窗
                    lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0);
                    lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
                    lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
                    fclose(fp);//关闭文件
                    return;
                }
            }
        }
    }
}

//返回登录事件
void go_back_event_cb(lv_event_t * e)
{
    lv_obj_clean(login);
    login_init();
}

//键盘事件
void keyboard_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if (code == LV_EVENT_FOCUSED) 
    {
        lv_keyboard_set_textarea(login_keyboard, obj);
        lv_obj_clear_flag(login_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
    else if (code == LV_EVENT_DEFOCUSED) 
    {
        lv_keyboard_set_textarea(login_keyboard, NULL);
        lv_obj_add_flag(login_keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}
//用户注册
void logon_event_cb(lv_event_t * e)
{
    lv_obj_clean(login);
    lv_obj_t *logon_title = lv_label_create(login);//注册标题
    logon_username_input = lv_textarea_create(login);//用户名输入框
    logon_password_input = lv_textarea_create(login);//密码输入框
    logon_password_input2 = lv_textarea_create(login);//密码输入框2
    logon_btn = lv_btn_create(login);//注册按钮
    lv_obj_t *logon_username= lv_label_create(login);//用户名文本
    lv_obj_t *logon_password = lv_label_create(login);//密码文本
    lv_obj_t *logon_password2 = lv_label_create(login);//密码文本2
    login_keyboard = lv_keyboard_create(login);//注册键盘
    lv_obj_t *logon_back_btn = lv_btn_create(login);//返回登录按钮

    jpg_bg = lv_img_create(login);
    lv_img_set_src(jpg_bg, "S:/imgs/352.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);

    //设置注册标题
    lv_label_set_text(logon_title, "用户注册");
    lv_obj_align(logon_title, LV_ALIGN_CENTER, 0, -200);
    lv_obj_set_style_text_color(logon_title, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(logon_title, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置用户名文本
    lv_label_set_text(logon_username, "用户名:");
    lv_obj_align(logon_username, LV_ALIGN_CENTER, -150, -100);
    lv_obj_set_style_text_color(logon_username, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(logon_username, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置密码文本
    lv_label_set_text(logon_password, "密码:");
    lv_obj_align(logon_password, LV_ALIGN_CENTER, -150, -50);
    lv_obj_set_style_text_color(logon_password, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(logon_password, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置密码文本2
    lv_label_set_text(logon_password2, "再次密码:");
    lv_obj_align(logon_password2, LV_ALIGN_CENTER, -150, 0);
    lv_obj_set_style_text_color(logon_password2, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(logon_password2, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置用户名输入框
    lv_obj_set_size(logon_username_input, 200, 30);
    lv_textarea_set_one_line(logon_username_input, true);
    lv_obj_align(logon_username_input, LV_ALIGN_CENTER, 0, -100);
    lv_textarea_set_placeholder_text(logon_username_input, "Enter your username");
    lv_obj_set_style_text_color(logon_username_input, lv_color_hex(0xCCCCCC), 0);
    //设置密码输入框
    lv_obj_set_size(logon_password_input, 200, 30);
    lv_textarea_set_one_line(logon_password_input, true);
    lv_obj_align(logon_password_input, LV_ALIGN_CENTER, 0, -50);
    lv_textarea_set_placeholder_text(logon_password_input, "Enter your password");
    lv_obj_set_style_text_color(logon_password_input, lv_color_hex(0xCCCCCC), 0);
    //设置密码输入框2
    lv_obj_set_size(logon_password_input2, 200, 30);
    lv_textarea_set_one_line(logon_password_input2, true);
    lv_obj_align(logon_password_input2, LV_ALIGN_CENTER, 0, 0);
    lv_textarea_set_placeholder_text(logon_password_input2, "Enter your password again");
    lv_obj_set_style_text_color(logon_password_input2, lv_color_hex(0xCCCCCC), 0);
    //设置注册按钮
    lv_obj_set_size(logon_btn, 200, 30);
    lv_obj_align(logon_btn, LV_ALIGN_CENTER, 0, 50);
    lv_obj_set_style_bg_color(logon_btn, lv_color_hex(0x0000ff), 0);
    //设置注册按钮文本
    lv_obj_t *logon_btn_label = lv_label_create(logon_btn);
    lv_label_set_text(logon_btn_label, "注册");
    lv_obj_center(logon_btn_label);
    lv_obj_set_style_text_color(logon_btn_label, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(logon_btn_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置注册键盘
    lv_obj_align(login_keyboard, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_flag(login_keyboard, LV_OBJ_FLAG_HIDDEN);
    //设置返回登录按钮
    lv_obj_set_size(logon_back_btn, 40, 40);
    lv_style_init(&go_back_btn_style);
    lv_style_set_radius(&go_back_btn_style, 0);
    lv_style_set_bg_color(&go_back_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&go_back_btn_style, LV_OPA_100);
    lv_obj_add_style(logon_back_btn, &go_back_btn_style, 0);
    //设置返回箭头label
    lv_obj_t *go_back_label = lv_label_create(logon_back_btn);
    lv_label_set_text(go_back_label, LV_SYMBOL_LEFT);
    lv_obj_center(go_back_label);
    //键盘事件处理
    lv_obj_add_event_cb(logon_username_input, keyboard_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(logon_password_input, keyboard_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(logon_password_input2, keyboard_event_cb, LV_EVENT_ALL, NULL);
    //返回登录事件
    lv_obj_add_event_cb(logon_back_btn, go_back_event_cb, LV_EVENT_CLICKED, NULL);
    //注册事件
    lv_obj_add_event_cb(logon_btn, go_logon_cb, LV_EVENT_CLICKED, NULL);

    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(login);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);
    
}

//登录事件处理
void login_event_cb(lv_event_t * e)
{
    char *username = lv_textarea_get_text(login_username_input);
    char *password = lv_textarea_get_text(login_password_input);

    if(check_user(username)==false)
    {
        logon_error_popup= lv_msgbox_create(login, "出错了", "用户名不存在", NULL, true);
        lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0); 
        lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
        lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_textarea_set_text(login_password_input, "");
        return;
    }
    else
    {
        if(check_user_password(username,password)==false)
        {
            logon_error_popup= lv_msgbox_create(login, "出错了", "密码错误", NULL, true);
            lv_obj_align(logon_error_popup, LV_ALIGN_CENTER, 0, 0); 
            lv_obj_set_style_text_font(logon_error_popup, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_add_event_cb(logon_error_popup, NULL, LV_EVENT_ALL, NULL);
            lv_textarea_set_text(login_password_input, "");
            return;
        }
        else
        {
            strcpy(as_username,username);
            printf("%s login success\n",as_username);
            //用户数据文件获取
            snprintf(user_path,sizeof(user_path),"/movie_user/%s.txt",as_username);
            printf("user_path:%s\n",user_path);
            FILE *fp = fopen(user_path, "r");
            //没有用户数据时则创建
            if(fp==NULL)
            {
                printf("新用户使用，正在创建用户数据\n");
                fp= fopen(user_path, "w");
                if(fp==NULL)
                {
                    lv_obj_t *error_popup = lv_msgbox_create(login, "出错了", "无法创建用户文件\n请联系管理员", NULL, true);
                    lv_obj_align(error_popup, LV_ALIGN_CENTER, 0, 0);
                    lv_obj_add_event_cb(error_popup, NULL, LV_EVENT_ALL, NULL);
                    return; // 终止登录流程
                }
                fclose(fp);
            }
            lv_obj_clean(login);
            if(strcmp(as_username,"admin")!=0)
            {
                ticket_init();
            }
            else
            {
                admin_init();
            }
            return;
        }
    }
}

// 初始化登录界面
void login_init(void)
{
    login = lv_obj_create(NULL);//登录主窗口
    lv_obj_t * login_title = lv_label_create(login);//登录标题
    lv_obj_t * login_username = lv_label_create(login);//用户名文本
    lv_obj_t * login_password = lv_label_create(login);//密码文本
    login_username_input = lv_textarea_create(login);//用户名输入框
    login_password_input = lv_textarea_create(login);//密码输入框
    login_btn = lv_btn_create(login);//登录按钮
    logon_btn = lv_btn_create(login);//注册按钮
    login_keyboard = lv_keyboard_create(login);//登录键盘
    //设置登录标题
    lv_label_set_text(login_title, "用户登录");
    lv_obj_set_style_text_font(login_title, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT); // 设置中文字体
    lv_obj_align(login_title, LV_ALIGN_CENTER, 0, -200);
    lv_obj_set_style_text_color(login_title, lv_color_hex(0x000000), 0);
    //设置登录主窗口
    lv_obj_set_size(login, 800, 480);
    lv_obj_set_style_bg_color(login, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(login, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(login, 2, 0);
    lv_obj_set_scroll_dir(login, LV_DIR_NONE);
    lv_obj_center(login);
    //设置图片背景
    jpg_bg = lv_img_create(login);
    lv_img_set_src(jpg_bg, "S:/imgs/352.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);
    //设置用户名文本
    lv_label_set_text(login_username, "用户名:");
    lv_obj_align(login_username, LV_ALIGN_CENTER, -150, -100);
    lv_obj_set_style_text_color(login_username, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(login_username, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT); 
    //设置密码文本
    lv_label_set_text(login_password, "密码:");
    lv_obj_align(login_password, LV_ALIGN_CENTER, -150, -50);
    lv_obj_set_style_text_color(login_password, lv_color_hex(0x000000), 0);
    lv_obj_set_style_text_font(login_password, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置用户名输入框
    lv_obj_set_size(login_username_input, 200, 30);
    lv_textarea_set_one_line(login_username_input, true);
    lv_obj_align(login_username_input, LV_ALIGN_CENTER, 0, -100);
    lv_textarea_set_placeholder_text(login_username_input, "Enter your username");
    lv_obj_set_style_text_color(login_username_input, lv_color_hex(0xCCCCCC), 0);
    //设置密码输入框
    lv_obj_set_size(login_password_input, 200, 30);
    lv_textarea_set_one_line(login_password_input, true);
    lv_textarea_set_password_mode(login_password_input, true);
    lv_obj_align(login_password_input, LV_ALIGN_CENTER, 0, -50);
    lv_textarea_set_placeholder_text(login_password_input, "Enter your password");
    lv_obj_set_style_text_color(login_password_input, lv_color_hex(0xCCCCCC),0);
    lv_scr_load(login);
    //设置登录按钮
    lv_obj_set_size(login_btn, 100, 50);
    lv_obj_align(login_btn, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x0000ff), 0);
    //设置登录按钮文本
    lv_obj_set_style_text_color(login_btn, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(login_btn, &lv_font_montserrat_20, 0);
    lv_obj_t * login_btn_label = lv_label_create(login_btn);
    lv_label_set_text(login_btn_label, "登录");
    lv_obj_center(login_btn_label);
    lv_obj_set_style_text_font(login_btn_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置注册按钮
    lv_obj_set_size(logon_btn, 100, 50);
    lv_obj_align(logon_btn, LV_ALIGN_CENTER, 0, 60);
    lv_obj_set_style_bg_color(logon_btn, lv_color_hex(0x0000ff), 0);
    //设置注册按钮文本
    lv_obj_set_style_text_color(logon_btn, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_text_font(logon_btn, &lv_font_montserrat_20, 0);
    lv_obj_t * logon_btn_label = lv_label_create(logon_btn);
    lv_label_set_text(logon_btn_label, "注册");
    lv_obj_center(logon_btn_label);
    lv_obj_set_style_text_font(logon_btn_label, &lv_font_source_han_sans_bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    //设置登录键盘
    lv_obj_align(login_keyboard, LV_ALIGN_BOTTOM_MID,0,0);
    lv_obj_add_flag(login_keyboard, LV_OBJ_FLAG_HIDDEN);
    //键盘事件处理
    lv_obj_add_event_cb(login_username_input, keyboard_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(login_password_input, keyboard_event_cb, LV_EVENT_ALL, NULL);
    //注册事件处理
    lv_obj_add_event_cb(logon_btn, logon_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(login_btn, login_event_cb, LV_EVENT_CLICKED, NULL);
    //设置今日日期
    get_date_str(now_Date, sizeof(now_Date));
    today= lv_label_create(login);
    lv_obj_align(today, LV_ALIGN_TOP_RIGHT, -20, 20);
    lv_label_set_text(today, now_Date);
    lv_obj_set_style_text_color(today, lv_color_hex(0x000000), 0);

}
